package cn.dhn_app.horseman_app.controller;

import cn.dhn_app.horseman_app.entity.token;
import cn.dhn_app.horseman_app.server.feign.FeignInterface;
import cn.itcast.util.ResponseEntity;
import cn.wx.WXPay;
import cn.wx.WXPayConstants;
import cn.wx.WXPayUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author lyy
 * @Description TODO
 * @PACKAGE_NAME: cn.dhn_app.horseman_app.controller
 * @ClassName WXPay_App
 * @Date 2020/10/15 16:40
 * @DAY_NAME_FULL: 星期四
 * @Version 1.0
 **/
@Slf4j
@RestController
@RequestMapping(value = "/dhn/wx_pay",produces = "application/json;charset=UTF-8")
public class WXPay_AppController {

    @Resource
    private FeignInterface feignTokenInterface;

    /**
     * 功能描述: <br>
     * 〈微信支付〉
     * @Author: lyy
     * @Date: 2020/10/15 16:45
     * @Param: [token, map]
     * @Return: cn.itcast.util.ResponseEntity
     */
    @RequestMapping(value = "horseman_Pay")
    public ResponseEntity horseman_App_Pay(@RequestHeader(value = "token")String token,
                                           @RequestBody Map<String,String> map){

        if(token.isEmpty()) return new ResponseEntity(401,"token参数为空！！！");
        token tokenEntity =this.feignTokenInterface.getToken(token);
        if(tokenEntity==null) return new ResponseEntity(402,"登陆超时,请重新登陆！！！");

        String openid = map.get("openId");

        String name = map.get("name");

        int total_fee = Integer.parseInt(map.get("total_fee"));

        WXPayConstants wxPayConstants =null;

        WXPay wxpay =null;

        //生成的随机字符串
        String nonce_str = WXPayUtil.generateNonceStr();
        //获取客户端的ip地址
        //获取本机的ip地址
        InetAddress addr = null;
        try {
            addr = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        String spbill_create_ip = addr.getHostAddress();
        //支付金额，需要转成字符串类型，否则后面的签名会失败
        //int  total_fee=1;
        //商品描述
        String body = "环保金充值";
        //商户订单号
        String out_trade_no= WXPayUtil.generateNonceStr();
        //统一下单接口参数配置
        HashMap<String, String> data = new HashMap<String, String>();
        data.put("appid",wxPayConstants.APP_ID);

        return null;
    }
}
